#include "ft6336u.h"
#include <stdio.h>

/**
 * @brief 复位 ft6336u
 * @param tp 触摸屏对象
 * @retval int 成功与否
 */
int ft6336u_reset(struct ft6336u* tp)
{
	if (NULL == tp) {
		return -1;
	}

	/* the spec requires at least 5ms */
	tp->ops.reset(LOW);
	tp->ops.msleep(10);

	/* the spec requires at least wait of 300ms */
	tp->ops.reset(HIGH);
	tp->ops.msleep(300);

	return 0;
}

/**
 * @brief 初始化 ft6336u
 * @param tp 触摸屏对象
 * @retval int 成功与否
 */
int ft6336u_init(struct ft6336u* tp)
{
	int ret = 0;
	uint8_t val = 0;

	if (NULL == tp) {
		return -1;
	}

	/* set touch panel 7bit i2c slave address */
	tp->cfg.fix.slave_address = FT6336U_I2C_7BIT_ADDR;

	/* reset sequence */
	ft6336u_reset(tp);

	/* enable the 近距离感应 */
	val = 0x01;
	ret = tp->ops.write(FT6336U_REG_ID_G_FACE_DEC_MODE, &val, 1);
	if (ret) {
		printf("err: ft6336u i2c write reg[0x%02x] failed!\n", FT6336U_REG_ID_G_FACE_DEC_MODE);
		return -1;
	}

	return 0;
}

/**
 * @brief 反初始化 ft6336u
 * @param tp 触摸屏对象
 */
void ft6336u_deinit(struct ft6336u* tp)
{
	if (NULL == tp) {
		return;
	}

	/* do nothing */
	return;
}

/**
 * @brief 读取 ft6336u 设备信息
 * @param tp 触摸屏对象
 * @param chip 返回的芯片ID
 * @param firmware 返回的固件版本
 * @param vendor 返回的厂商ID
 * @param version 返回的库版本
 */
int ft6336u_get_info(struct ft6336u* tp, uint32_t* chip, uint8_t* firmware, uint8_t* vendor, uint16_t* version)
{
	int ret = 0;
	uint8_t val[3] = { 0 };

	/* parameter verify */
	if (NULL == tp || NULL == chip || NULL == firmware || NULL == vendor || NULL == version) {
		return -1;
	}

	/* read chip id */
	ret = tp->ops.read(FT6336U_REG_ID_G_CIPHER_HIGH, &val[0], 1);
	if (ret) {
		printf("err: ft6336u i2c read reg[0x%02x] failed!\n", FT6336U_REG_ID_G_CIPHER_HIGH);
		return -1;
	}

	ret = tp->ops.read(FT6336U_REG_ID_G_CIPHER_MIDE, &val[1], 2);
	if (ret) {
		printf("err: ft6336u i2c read reg[0x%02x] failed!\n", FT6336U_REG_ID_G_CIPHER_MIDE);
		return -1;
	}
	*chip = (val[0] << 16) + (val[1] << 8) + val[2];

	/* read firmware id */
	ret = tp->ops.read(FT6336U_REG_ID_G_FIRMID, &val[0], 1);
	if (ret) {
		printf("err: ft6336u i2c read reg[0x%02x] failed!\n", FT6336U_REG_ID_G_FIRMID);
		return -1;
	}
	*firmware = val[0];

	/* read vendor id */
	ret = tp->ops.read(FT6336U_REG_ID_G_FOCALTECH_ID, &val[0], 1);
	if (ret) {
		printf("err: ft6336u i2c read reg[0x%02x] failed!\n", FT6336U_REG_ID_G_FOCALTECH_ID);
		return -1;
	}
	*vendor = val[0];

	/* read lib version */
	ret = tp->ops.read(FT6336U_REG_ID_G_LIB_VERSION_H, &val[0], 2);
	if (ret) {
		printf("err: ft6336u i2c read reg[0x%02x] failed!\n", FT6336U_REG_ID_G_LIB_VERSION_H);
		return -1;
	}
	*version = (val[0] << 8) | val[1];

	return 0;
}

/**
 * @brief 读取 ft6336u 触点信息
 * @param tp 触摸屏对象
 * @param n 返回的芯触摸点个数
 * @param x 返回的x坐标数组(最多两点)
 * @param y 返回的y坐标数组(最多两点)
 * @retval int 成功与否
 */
int ft6336u_get_coord(struct ft6336u* tp, uint8_t* n, uint16_t* x, uint16_t* y)
{
	int ret = 0;
	uint8_t num = 0;
	uint8_t val[16] = { 0 };

	if (NULL == tp || NULL == n || NULL == x || NULL == y) {
		return -1;
	}

	ret = tp->ops.read(0x00, val, 16);
	if (ret) {
		return -1;
	}

	/* 触摸点个数 */
	num = val[2];
	*n = num;

	/* 触摸点坐标 */
	switch (num) {
	case 0:
		*x = 0;
		*y = 0;
		*(x + 1) = 0;
		*(y + 1) = 0;
		break;
	case 1:
		*x = ((val[3] & 0xf) << 8) | val[4];
		*y = ((val[5] & 0xf) << 8) | val[6];
		*(x + 1) = 0;
		*(y + 1) = 0;
		break;
	case 2:
		*x = ((val[3] & 0xf) << 8) | val[4];
		*y = ((val[5] & 0xf) << 8) | val[6];
		*(x + 1) = ((val[9] & 0xf) << 8) | val[10];
		*(y + 1) = ((val[11] & 0xf) << 8) | val[12];
		break;
	default:
		break;
	}

	return 0;
}